home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gcctest / tests03.zoo / ttime.c < prev   
C/C++ Source or Header  |  1992-04-27  |  3KB  |  122 lines

  1. /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include "ansidecl.h"
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <time.h>
  23. #ifdef _BSD
  24. #include <sys/types.h>
  25. #endif
  26.  
  27. int
  28. DEFUN(main, (argc, argv), int argc AND char **argv)
  29. {
  30.   int margc;
  31.   char *margv[3];
  32.  
  33.   margc = 2;
  34.   margv[0] = argv[0];
  35.   margv[1] = "EST5EDT";
  36.   margv[2] = NULL;
  37.   return do_test_time(margc, margv);
  38. }
  39.  
  40. int
  41. DEFUN(do_test_time, (argc, argv), int argc AND char **argv)
  42. {
  43.   time_t t;
  44.   register struct tm *tp;
  45.   struct tm tbuf;
  46.   int lose = 0;
  47.  
  48.   --argc;
  49.   ++argv;
  50.  
  51.   do
  52.     {
  53.       char buf[BUFSIZ];
  54.       if (argc > 0)
  55.     {
  56.       static char buf[BUFSIZ];
  57.       sprintf(buf, "TZ=%s", *argv);
  58.       if (putenv(buf))
  59.         {
  60.           puts("putenv failed.");
  61.           lose = 1;
  62.         }
  63.       else
  64.         puts (buf);
  65.     }
  66.       tzset();
  67.       tbuf.tm_year = 72;
  68.       tbuf.tm_mon = 0;
  69.       tbuf.tm_mday = 31;
  70.       tbuf.tm_hour = 6;
  71.       tbuf.tm_min = 14;
  72.       tbuf.tm_sec = 50;
  73.     doit:;
  74.       t = mktime(&tbuf);
  75.       if (t == (time_t) -1)
  76.     {
  77.       puts("mktime() failed?");
  78.       lose = 1;
  79.     }
  80.       tp = localtime(&t);
  81.       if (tp == NULL)
  82.     {
  83.       puts("localtime() failed.");
  84.       lose = 1;
  85.     }
  86.       else if (strftime(buf, sizeof(buf), "%a %b %d %X %Z %Y", tp) == 0)
  87.     {
  88.       puts("strftime() failed.");
  89.       lose = 1;
  90.     }
  91.       else
  92.     puts(buf);
  93.       if (tbuf.tm_year == 101)
  94.     {
  95.       tbuf.tm_year = 97;
  96.       tbuf.tm_mon = 0;
  97.       goto doit;
  98.     }
  99.       ++argv;
  100.     } while (--argc > 0);
  101.  
  102.   {
  103. #define    SIZE    256
  104.     char buffer[SIZE];
  105.     time_t curtime;
  106.     struct tm *loctime;
  107.  
  108.     curtime = time (NULL);
  109.  
  110.     loctime = localtime (&curtime);
  111.  
  112.     fputs (asctime (loctime), stdout);
  113.  
  114.     strftime (buffer, SIZE, "Today is %A, %B %d.\n", loctime);
  115.     fputs (buffer, stdout);
  116.     strftime (buffer, SIZE, "The time is %I:%M %p.\n", loctime);
  117.     fputs (buffer, stdout);
  118.   }
  119.  
  120.   return(lose ? EXIT_FAILURE : EXIT_SUCCESS);
  121. }
  122.